package com.ruoyi.oto.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.oto.constants.AnnoType;
import com.ruoyi.oto.constants.MemberRoleConstant;
import com.ruoyi.oto.domain.*;
import com.ruoyi.oto.domain.dto.AddAnnotationDTO;
import com.ruoyi.oto.mapper.DemandsAnnoMapper;
import com.ruoyi.oto.mapper.DemandsReplyMapper;
import com.ruoyi.oto.service.CompanyCoreService;
import com.ruoyi.oto.service.DemandsAnnoService;
import com.ruoyi.oto.service.DemandsDepartService;
import com.ruoyi.oto.service.DemandsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 诉求批注服务
 *
 * @author ruoyi
 */
@Service
public class DemandsAnnoServiceImpl extends ServiceImpl<DemandsAnnoMapper, DemandsAnno> implements DemandsAnnoService
{
    @Autowired
    private DemandsRecordService demandsRecordService;

    @Autowired
    private DemandsDepartService demandsDepartService;

    @Autowired
    private CompanyCoreService companyCoreService;
    @Autowired
    private DemandsAnnoService demandsAnnoService;

    /**
     * 新增诉求相关的批注
     * @param memberUser
     * @param addAnnotationDTO
     */
    @Override
    public void saveDemandsAnno(MemberUser memberUser, AddAnnotationDTO addAnnotationDTO) {
        //根据memberUser来区分校验
        Long roleId = memberUser.getRoleId();
        if(MemberRoleConstant.联企用户.equals(roleId)){
            DemandsRecord record = demandsRecordService.getById(addAnnotationDTO.getDemandId());
            if(!memberUser.getId().equals(record.getMemberId())){
                throw new ServiceException("找不到对应的诉求记录");
            }

        }
        else if(MemberRoleConstant.企业用户.equals(roleId)){
            DemandsRecord record = demandsRecordService.getById(addAnnotationDTO.getDemandId());
            Long companyId = record.getCompanyId();
            CompanyCore companyCore = companyCoreService.getById(companyId);
            if(!memberUser.getId().equals(companyCore.getMemberId())){
                throw new ServiceException("找不到对应的诉求记录");
            }
        }

        if(AnnoType.DEMANDS_DEPART.equals(addAnnotationDTO.getAnnoType())){  //如果是针对诉求回复
            DemandsDepart demandsDepart =  demandsDepartService.getById(addAnnotationDTO.getDmdDptId());
            if(ObjectUtil.isNull(demandsDepart)){
                throw new ServiceException("找不到部门诉求处置记录");
            }
        }
        else if(AnnoType.DEPART_REPLY.equals(addAnnotationDTO.getAnnoType())){
            DemandsReply demandsReply = demandsDepartService.getDemandsReply(addAnnotationDTO.getReplyId());
            if(ObjectUtil.isNull(demandsReply)){
                throw new ServiceException("找不到部门回复记录");
            }
        }
        DemandsAnno demandsAnno = new DemandsAnno();
        demandsAnno.setAnnoType(addAnnotationDTO.getAnnoType());
        demandsAnno.setContent(addAnnotationDTO.getContent());
        demandsAnno.setRefId(AnnoType.DEMANDS_DEPART.equals(addAnnotationDTO.getAnnoType())?addAnnotationDTO.getDmdDptId():addAnnotationDTO.getReplyId());
        demandsAnno.setRoleId(roleId.intValue());
        demandsAnno.setMemberId(memberUser.getId());
        demandsAnno.setCreateBy(memberUser.getUsername());
        demandsAnno.setCreateTime(new Date());
        demandsAnno.insert();
    }

    /**
     * 删除批注
     * @param memberUser
     * @param id
     */
    @Override
    public void deleteDemandsAnno(MemberUser memberUser, Long id) {
        DemandsAnno demandsAnno = this.getById(id);
        if(ObjectUtil.isNull(demandsAnno) || !memberUser.getId().equals(demandsAnno.getMemberId())){
            throw new ServiceException("找不到对应的数据");
        }
        else{
            this.removeById(demandsAnno.getId());
        }
    }

    @Override
    public List<DemandsAnno> listAnnotation(MemberUser memberUser, Long demandId,Long refId) {
        //根据memberUser来区分校验
        Long roleId = memberUser.getRoleId();
        if(MemberRoleConstant.联企用户.equals(roleId)){
            DemandsRecord record = demandsRecordService.getById(demandId);
            if(!memberUser.getId().equals(record.getMemberId())){
                throw new ServiceException("找不到对应的诉求记录");
            }
        }
        else if(MemberRoleConstant.企业用户.equals(roleId)){
            DemandsRecord record = demandsRecordService.getById(demandId);
            Long companyId = record.getCompanyId();
            CompanyCore companyCore = companyCoreService.getById(companyId);
            if(!memberUser.getId().equals(companyCore.getMemberId())){
                throw new ServiceException("找不到对应的诉求记录");
            }
        }
        else if(MemberRoleConstant.游客.equals(roleId)){ throw new ServiceException("非法访问");}
        return this.getBaseMapper().getAllByRefId(refId);
    }
}
